﻿using System;
using System.Windows.Forms;

using DreamCube.WinForm.Common;
using DreamCube.Foundation.Basic.Utility;

namespace ShopHelper.UI.UC
{
    public partial class CopyGood1 : UserControl
    {
        #region "字段"

        BLL.ItemHtmlElement currentContextItem;

        #endregion

        public CopyGood1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 单击前往按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripBtn_Go_Click(object sender, EventArgs e)
        {
            this.webBrowser.Navigate(toolStripTxt_Url.Text);
        }

        /// <summary>
        /// 右键菜单触发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void doc_ContextMenuShowing(object sender, HtmlElementEventArgs e)
        {
            try
            {
                HtmlElement contextMenuSelectedElement = webBrowser.Document.GetElementFromPoint(e.ClientMousePosition);
                if (contextMenuSelectedElement == null) return;
                currentContextItem = BLL.ShopFacotry.CreateItemHtmlElement(contextMenuSelectedElement);
                if (currentContextItem.IsGoodElement())
                    contextMenuStrip1.Show(webBrowser, e.ClientMousePosition);
                else
                    contextMenuSelectedElement = null;
            }
            catch (Exception ex)
            {
                MyLog.MakeLog(ex);
            }
        }

        /// <summary>
        /// 文档加载完毕之后就增加右键事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            HtmlDocument doc = webBrowser.Document;
            if (doc != null)
            {
                doc.ContextMenuShowing += new HtmlElementEventHandler(doc_ContextMenuShowing);
            }
        }

        /// <summary>
        /// 复制商品按钮的单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 复制此商品ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                String itemID; //商品ID
                String itemTitle; //商品标题
                String itemFrom;//商品来源 
                String itemUrl; //商品的Url地址
                if (currentContextItem == null) return;
                currentContextItem.ParseHtmlElement(out itemTitle, out itemFrom, out itemUrl, out itemID);
                MyControl.ControlInvoke(this, new Action<Object>((g) =>
                {
                    //判断是否已经加到了界面上
                    for (Int32 i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        if (dataGridView1.Rows[i].Cells[2].Value.ToString() == itemID)
                            return;
                    }
                    Object[] attr = new Object[] { dataGridView1.Rows.Count + 1, itemFrom, itemID, itemTitle, itemUrl, "删除" };
                    dataGridView1.Rows.Add(attr);
                }), "");
            }
            catch (Exception ex)
            {
                MyLog.MakeLog(ex);
            }
        }

        /// <summary>
        /// 单击单元格内容的时候触发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 5)
            {
                if (MessageBox.Show("确定要删除选中记录吗？", "删除提醒") == DialogResult.OK)
                {
                    dataGridView1.Rows.RemoveAt(e.RowIndex);
                    ResetRowIndex();
                }
            }
        }

        /// <summary>
        /// 重置行号
        /// </summary>
        private void ResetRowIndex()
        {
            for (Int32 i = 0; i < dataGridView1.Rows.Count; i++)
                dataGridView1.Rows[i].Cells[0].Value = i + 1;
        }

        /// <summary>
        /// 按下回车键的时候
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripTxt_Url_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.webBrowser.Navigate(this.toolStripTxt_Url.Text);
            }
        }

        /// <summary>
        /// 点击超链接的时候避免弹开窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void webBrowser_NewWindow(object sender, System.ComponentModel.CancelEventArgs e)
        {
            String url = webBrowser.Document.ActiveElement.GetAttribute("href");
            if (!String.IsNullOrEmpty(url))
            {
                toolStripTxt_Url.Text = url;
                webBrowser.Navigate(url);
            }
            e.Cancel = true;
        }
    }
}
